package com.aaa.ddd.controller;


import com.aaa.pojo.LayUiTree;
import com.aaa.pojo.Menu;
import com.aaa.pojo.ResultBean;
import com.aaa.pojo.Role;

import com.aaa.utils.RetStatusEnum;

import com.aaa.ddd.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    public  ResultBean getSuccessResult(ResultBean resultBean) {
        if(null==resultBean){
            resultBean= new ResultBean();
        }
        resultBean.setCode(RetStatusEnum.SUCCESS.getCode());
        resultBean.setMsg(RetStatusEnum.SUCCESS.getMessage());
        System.out.println(resultBean);
        return resultBean;
    }


    public  ResultBean getFailResult(ResultBean resultBean) {
        if(null==resultBean){
            resultBean= new ResultBean();
        }
        resultBean.setCode(RetStatusEnum.FAILE.getCode());
        resultBean.setMsg(RetStatusEnum.FAILE.getMessage());
        return resultBean;
    }


    @Autowired
    private RoleService roleService;

    @RequestMapping("/pagination")
    public ResultBean findAll(String page, String limit){

        List<Role> all = roleService.findAll(page, limit);
        int count = roleService.findCount();
        ResultBean resultBean = new ResultBean();
        resultBean.setCode(0);
        resultBean.setMsg("");
        resultBean.setCount(count);
        resultBean.setData(all);
        return resultBean;

    }


    @RequestMapping("/delete")
    public ResultBean delete( int roleId){
        System.out.println(roleId);
        int delete = roleService.delete(roleId);
        ResultBean resultBean = new ResultBean();

        if(delete != 0){
            resultBean.setCode(0);
            resultBean.setMsg("删除成功");
        }else{
            resultBean.setCode(1);
            resultBean.setMsg("删除失败");
        }
        return resultBean;
    }

    @GetMapping("/findSelectMenu")
    public List<Integer> findSelectMenu(int roleId){
        return  roleService.findSelectMenu(roleId);
    }

    @PostMapping("/insertRole")
    public ResultBean insertRole(Role role,String menus){
        ResultBean resultBean = new ResultBean();
        Role insert = roleService.insert(role);
        if(insert != null){
            resultBean.setCode(0);
            resultBean.setMsg("添加成功");
        }else {
            resultBean.setMsg("添加失败");
            resultBean.setCode(1);
        }
        return resultBean;

    }

    @RequestMapping("/updateRole")
    public ResultBean updateRole(Role role ,String menus){
        Role update = roleService.update(role);
        ResultBean resultBean = new ResultBean();
        if(update != null){
            resultBean.setCode(0);
            resultBean.setMsg("修改成功");
        }else {
            resultBean.setCode(1);
            resultBean.setMsg("修改失败");
        }
        return resultBean;
    }


    @GetMapping("/findAllMenu")
    public List<LayUiTree> findAllMenu(){
        List<Menu> menus = roleService.findAllMenu();
        List<LayUiTree> list = new ArrayList<>();
        for (Menu menu : menus) {
            if(menu.getParentId() == 0){
                LayUiTree tree = new LayUiTree();
                tree.setId(menu.getMenuId());
                tree.setTitle(menu.getMenuName());
                tree.setUrl(menu.getUrl());
                tree.setChildren(getChildren(menu.getMenuId(),menus));

                tree.setChildren(getChildren(menu.getMenuId(), menus));
                list.add(tree);
            }
        }
        return list;
    }


    public List<LayUiTree> getChildren(Integer parentId, List<Menu> menus) {
        List<LayUiTree> list = new ArrayList<>();
        for (Menu menu : menus) {
            if (menu.getParentId().equals(parentId)) {
                LayUiTree tree = new LayUiTree();
                tree.setId(menu.getMenuId());
                tree.setTitle(menu.getMenuName());
                tree.setUrl(menu.getUrl());
                tree.setChildren(getChildren(menu.getMenuId(), menus));
                list.add(tree);
            }
        }
        return list;
    }


}
